package middleware

import (
	"errors"
	"github.com/gin-gonic/gin"
	"shangke-device/app/svcContent"
	"shangke-device/internal/result"
)

const authorizedKey = "mqtt"

func AuthorizedMiddleware(svc *svcContent.SvcContent) gin.HandlerFunc {
	return func(c *gin.Context) {
		secretKey := c.Request.Header.Get(authorizedKey)
		if secretKey != svc.Conf.SecretKey {
			result.JsonResult(c, nil, errors.New("密钥错误,无权限访问"))
			c.Abort()
			return
		}

		c.Next()
	}
}
